//模块声明和导入
var bwApp = angular.module('bwApp', [
    'bw.controller',
    'bw.services',
    'ui.router',
    'ngCookies',
    'bw.filters',
    'bw.directive'
]);


bwApp.config(['$urlRouterProvider', '$stateProvider', '$httpProvider', function($urlRouterProvider, $stateProvider, $httpProvider) {
    // 路由配置
    $stateProvider
        .state('default_index', {
            url: '/index',
            /*controller: 'indexCtrl',*/
            stateName: '预约选择',
            templateUrl: "views/choose.html"
        })
        .state('appointment', {
            url: '/appointment/:type',
            controller: 'appointmentCtrl',
            stateName: '我要预约',
            templateUrl: "views/searchFlight.html",
            cache: true
        })
        .state('searchOrder', {
            url: '/searchOrder',
            controller: 'searchOrder',
            stateName: '查询预约',
            templateUrl: "views/searchOrder.html",
            cache: true
        })
        .state('appointment.chooseCity', {
            url: '/chooseCity',
            stateName: '选择城市',
            views: {
                "chooseCity": {
                    controller: "chooseCityCtrl",
                    templateUrl: "views/city.html"
                }
            }
        })
        .state('introduce', {
            url: '/introduce',
            stateName: '接送机说明',
            templateUrl: "views/introduce.html",
            cache: true
        })
        .state('createAppointment', {
            url: '/createAppointment/:pId/:type',
            controller: 'addAppointmentCtrl',
            stateName: '填写预约资料',
            templateUrl: "views/appointment.html",
            cache: true
        }).state('appointmentList', { //该微信号的预约记录
            url: '/appointmentList',
            controller: 'appointmentListCtrl',
            stateName: '预约记录',
            templateUrl: "views/appointmentList.html",
            cache: true
        }).state('cancel', { //取消预约记录
            url: '/cancel/:blId',
            controller: 'cancelCtrl',
            stateName: '取消预约',
            templateUrl: "views/cancel.html",
            cache: true
        }).state('cancelResult', { //取消预约成功后的结果展现
            url: '/cancelResult/:blId',
            controller: 'appCancelResultCtrl',
            stateName: '已取消',
            templateUrl: "views/cancelResult.html",
            cache: true
        }).state('ongoing', { //取消预约成功后的结果展现
            url: '/ongoing/:blId/:noAction',
            controller: 'ongoingCtrl',
            stateName: '进行中',
            templateUrl: "views/ongoing.html",
            cache: true
        }).state('paidan', { //派单中
            url: '/paidan/:blId/:stateName/:noAction',
            controller: 'appointmentInfoCtrl',
            stateName: '派单中',
            templateUrl: "views/paidaning.html",
            cache: true
        });
    //默认访问的页面地址
    $urlRouterProvider.otherwise('/index');
    //上传文件http配置信息
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
    $httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript';
    // $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
    $httpProvider.defaults.withCredentials = true;

    // 配置http拦截器
    $httpProvider.interceptors.push('httpInterceptor');


}]);
bwApp.run(['$rootScope', '$state', '$cookieStore', function($rootScope, $state, $cookieStore) {
    //路由拦截器切换之前
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        if (window._token || JSON.parse(localStorage.getItem("_token"))) {
            //授权成功过
        } else {
            if (location.href.indexOf("hasLoadOnce=true") > -1) {
                return true;
            } else {
                /*location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe69910f2a2cfd1a1&redirect_uri=" +
                    encodeURIComponent(location.href + "?hasLoadOnce=true") + "&response_type=code&scope=snsapi_userinfo&state=11#wechat_redirect";*/

            }
        }
    });
    //路由切换成功之后
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
        document.body.scrollTop = 0; //置顶
        document.title = toState.stateName; //设置title  android 直接就可以修改成功
        if (toState.name == "paidan") {
            document.title = toParams.stateName;
        }
        //微信中的浏览器中单页面第一次加载成功后，不再监听tile的变化,通过iframe来触发 页面重新来监听
        var $body = $('body');
        var $iframe = $("<iframe style='opacity: 0;' src='/favicon.ico'></iframe>");
        $iframe.on('load', function() {
            setTimeout(function() {
                $iframe.off('load').remove();
            }, 0);
        }).appendTo($body);
    });
    //post请求时需要格式化参数（编码和序列化）
    window.param = function(obj) {
        return JSON.stringify(obj);
    };
    var RootFontSize = document.body.offsetWidth / 750 * 16;
    document.getElementsByTagName("html")[0].style.fontSize = RootFontSize + "px";
    window.onresize = function() {
        RootFontSize = document.body.offsetWidth / 750 * 16;
        document.getElementsByTagName("html")[0].style.fontSize = RootFontSize + "px";
    }
    FastClick.attach(document.body);
}]);
angular.module('bw.controller', ['bw.services','infinite-scroll']);
angular.module('bw.services', ['bw.config', 'ngResource', 'bw.views']);
angular.module('bw.directive', ['bw.services', 'bw.views']);
angular.module('bw.filters', []);
